'use strict';

const path = require('path')
const pkgDir = require('pkg-dir').sync
const { isObject } = require('@lio-cli/utils')

class Package {
    constructor (options) {
        console.log('Package constructor')

        if (!options) {
            throw new Error('Package类的options参数不能为空！')
        }
        if (!isObject(options)) {
            throw new Error('Package类的options参数必须为对象！')
        }
        // Package的目标路径
        this.targetPath = options.targetPath
        // // Package的缓存路径
        // this.storePath = options.storePath
        // package的name
        this.packageName = options.packageName
        // package的Version
        this.packageVersion = options.packageVersion
    }

    // 判断当前的Package是否存在
    exists() {

    }

    // 安装Package
    install () {

    }

    // 更新Package
    update() {

    }

    // 获取入口文件的路径
    getRootFilePath () {
        // 1、获取package.json所在目录 - pkg-dir
        const dir = pkgDir(this.targetPath)
        console.log('dir =======>', dir)
        if (dir) {
            // 2、读取package.json - required()
            const pkgFile = require(path.resolve(dir, 'package.json'))
            console.log('pkgFile =======>', pkgFile)
            // 3、寻找main/lib
            if (pkgFile && pkgFile.main) {
                // 4、路径的兼容(maxOs/windows)
                return path.resolve(dir, pkgFile.main)
            }
        }
        return null
    }
}

module.exports = Package;

